define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hr/overtime/index' + location.search,
                    table: 'hr_overtime',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable: true},
                        {field: 'user_name', title: __('员工姓名')},
                        {field: 'overtime_date', title: __('加班日期'), sortable: true},
                        {field: 'overtime_type_text', title: __('加班类型')},
                        {field: 'start_time_text', title: __('开始时间')},
                        {field: 'end_time_text', title: __('结束时间')},
                        {field: 'overtime_hours', title: __('加班时长（小时）')},
                        {field: 'reason', title: __('加班原因'), formatter: function(value) {
                            return value ? value.substr(0, 30) + '...' : '';
                        }},
                        {field: 'status_text', title: __('状态'), formatter: function(value, row) {
                            var colorMap = {
                                '待审批': 'warning',
                                '已批准': 'success',
                                '已拒绝': 'danger'
                            };
                            var color = colorMap[value] || 'default';
                            return '<span class="label label-' + color + '">' + value + '</span>';
                        }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                            {
                                name: 'detail',
                                text: __('详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-list',
                                url: 'hr/overtime/detail',
                                extend: 'data-area=\'["90%", "90%"]\''
                            },
                            {
                                name: 'approve',
                                text: __('审批'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-check',
                                url: 'hr/overtime/approve',
                                extend: 'data-area=\'["600px", "400px"]\'',
                                visible: function(row) {
                                    return row.status == 0;
                                }
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
